package watch_cmd

import (
	"learning/gooop/spring/autogen/command"
	"os"
	"strings"
)

// AddCmd calls service.WatchAdd() to add dir to watch list
type AddCmd struct {
	dir string
}

type AddCmdBuilder int

var gAddCmdPrefix = "watch add "

func (me *AddCmd) String() string {
	return gAddCmdPrefix + me.dir
}

func (me *AddCmd) Apply(c command.ICmdContext) error {
	// todo: fixme
	panic("implements me")
}

func (me *AddCmdBuilder) Build(line string) (error, command.ICmd) {
	// check prefix
	if !strings.HasPrefix(line, gAddCmdPrefix) {
		return nil, nil
	}

	// get dir
	dir := strings.TrimSpace(line[len(gAddCmdPrefix):])

	// check dir
	_, e := os.Stat(dir)
	if e != nil {
		return e, nil
	}

	// ok
	return nil, &AddCmd{dir}
}
